home *** CD-ROM | disk | FTP | other *** search
/ Universität Tübingen - 1997/1998 Winter / Universität Tübingen - Wintersemester 1997-98 - Uni-Informationssystem und Stadt-Informationssystem.iso / knl / search.pl < prev    next >
Perl Script  |  1997-08-14  |  4KB  |  161 lines

  1. #!/bin/perl
  2. # SEARCH.PL    This Perl-script is called from the HTML-form
  3. #    "SEARCH_CC.HTML", and is intended to look up certain patterns in the
  4. #    file specified in "/info/www/data/uni/knl/txt/[name].txt", where [name] is 
  5. #    the name of the input-form-field.
  6. #    Only plain characters and numbers are allowed (line 86).
  7. #
  8. #    ThH, July-97
  9. #    Ver 1.2
  10. #
  11.  
  12. ###############################################################################
  13. # Return html-header
  14. ###############################################################################
  15. #
  16. sub htmlheader {
  17. print STDOUT <<EOD;
  18. Content-type: text/html
  19.  
  20. <html>
  21. <head>
  22. <title>Search - Results</title>
  23. </head>
  24. <body>
  25. <pre>
  26. EOD
  27. }
  28.  
  29. ###############################################################################
  30. # Message for invalid input:
  31. ###############################################################################
  32. sub wrong_input {
  33. print STDOUT <<EOD;
  34. <BODY>
  35. <STRONG>
  36. Your request could not be handled, since it contains non-valid characters.
  37. Please
  38. <A HREF="http://www.uni-tuebingen.de/volab/search_cc.html">try again</A> 
  39. , using only the characters A-Z, a-z, 0-9, and "-".
  40. </STRONG>
  41. </BODY>
  42. </HTML>
  43. EOD
  44. }
  45.  
  46. ###############################################################################
  47. #
  48. # main program
  49. #
  50. ################################################################################
  51. sub main {
  52.     
  53.     # location of the file with the information:
  54.     
  55.     &htmlheader;     # Header for Output
  56.     
  57.     $error = 0;
  58.         # $error == 0 keine Sonderzeichen
  59.         # $error == 1 Sonderzeichen
  60.     $input = "";
  61.     
  62.     # ------------------------------------------------------------
  63.     if ($ENV{'REQUEST_METHOD'} eq "POST") { 
  64.         read(STDIN,$input,$ENV{"CONTENT_LENGTH"});
  65.     }
  66.     else {
  67.     # when called with PUT:
  68.     
  69.         $input=$ENV{"QUERY_STRING"};
  70.     };
  71.     
  72.     # $input hat jetzt den String des Formulars -> decodieren, 
  73.     # syntaktisch testen und abspeichern
  74.     # ------------------------------------------------------------
  75.     $idx=0;
  76.     foreach $paar(split(/&/,$input)) {
  77.         ($name,$value)=split(/=/,$paar);
  78.         $value=~ tr/+/ /;
  79.         $name=~ tr/+/ /;
  80.         $value =~ s/%(..)/pack("c",hex($1))/ge;
  81.     
  82.     # $value auf spezielle Shell-Zeichen testen
  83.     # $value auf nonprintable ASCII < 32(dezimal) testen
  84.     # ------------------------------------------------------------
  85.         if ( ($value =~ /[;><&\*`\|\\]/) ||  
  86.              ($value =~ /[\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]/ ) 
  87.            ) 
  88.         {
  89.             # print ("+++ACHTUNG: $value \n");
  90.             $error = 1; 
  91.             goto CONTINUE; # ungueltiges Zeichen, weitere Werte nicht pruefen;
  92.                         # Sprung zu Label CONTINUE
  93.         };
  94.     
  95.         $var_form[$idx] = $name;
  96.         $val_form[$idx] = $value;
  97.         $idx++;
  98.     }
  99.     
  100.     # Sprung hierher im Fehlerfall oder sequentiell bei Nichtfehlerfall
  101.     # ------------------------------------------------------------
  102.     CONTINUE:
  103.     
  104.     # $error == 1 ist Fehlerfall ungueltiges Zeichen
  105.     # ------------------------------------------------------------
  106.     if ($error == 1) {
  107.         # Reply to user:
  108.         &wrong_input;
  109.     } 
  110.     else { # $error == 0;
  111.         # everything ok -> find information, and return it
  112.     } 
  113.     
  114.     # --------------------------------------------
  115.     for ($idx2 = 0; $idx2 < $idx; $idx2++) {
  116.         # Pass the values on the the folling search-routine:
  117.         $_ = $val_form[$idx2];
  118.         
  119.         # Open the file with the information:
  120.         # --------------------------------------------
  121.         $infile = "/info/www/data/uni/knl/txt/$var_form[$idx2].txt";
  122.         open(IN_FILE, "<$infile");
  123.         
  124.         print STDOUT "<h1>$var_form[$idx2] - Results</h1>\n";
  125.         print STDOUT "<p>The Following information has been found to match <I>$_</I>:</p>\n";
  126.         print STDOUT "<hr>\n";
  127.         $search = $_;
  128.         $line = "\n-----------------------\n";
  129.         while(<IN_FILE>) {
  130.             if (/_EOI_/) {
  131.                 if (grep /$search/, @info) {
  132.                     for ($i=0; $i<$index; $i++) {
  133.                         $return_info[$ret_index++] = $info[$i];
  134.                     }        
  135.                     $return_info[$ret_index++] = $line;
  136.                 }
  137.                 undef @info;
  138.                 undef $index;
  139.             }
  140.             else {
  141.                 $info[$index++] = $_;
  142.             }
  143.         }
  144.         
  145.         # Finish everything off properly.
  146.         # --------------------------------------------
  147.         print STDOUT @return_info;
  148.         close IN_FILE;
  149.         
  150.     }    # for ($idx2 ...
  151.     
  152.     exit 0
  153.     
  154. }    # main {
  155.  
  156. #*****************************************************************
  157.  
  158. &main;
  159.  
  160. #*****************************************************************
  161.